package com.wang.java.basic.framework.queue.blockingqueue;

import com.wang.common.domain.DelayedUser;

import java.util.concurrent.DelayQueue;

/**
 * 延时队列，基于Delayed接口和PriorityQueue实现
 * 数据结构：链表
 * 线程安全：是，基于ReentrantLock实现
 * 队列长度：无界
 * 阻塞性：阻塞
 */
public class DelayQueueTest {
    public static void main(String[] args) throws InterruptedException {
        DelayQueue<DelayedUser> delayQueue = new DelayQueue<>();
        // 消费端
        new Thread(() -> {
            while (true) {
                DelayedUser delayedUser = null;
                try {
                    delayedUser = delayQueue.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (delayedUser != null) {
                    System.out.println(delayedUser);
                }
            }
        }).start();
        // 生产端
        delayQueue.put(new DelayedUser(3, "王五", 3000));
        delayQueue.put(new DelayedUser(1, "张三", 5000));
        delayQueue.put(new DelayedUser(2, "李四", 10000));
    }
}
